//
//  RightTableView.m
//  hai游～
//
//  Created by jakf_17 on 15/5/26.
//  Copyright (c) 2015年 jakf_17. All rights reserved.
//

#import "RightTableView.h"
#import "ReViewsCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
@implementation RightTableView


- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createMytableView];
    }
    return self;
}
- (void)createMytableView
{
    self.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, self.frame.size.height - 114) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.separatorColor = [UIColor colorWithRed:210 / 255.0 green:210 / 255.0 blue:210 / 255.0 alpha:1];
    [self addSubview:_tableView];
    [_tableView release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
    NSDictionary *review = _array[indexPath.row];
    NSString *content = [review objectForKey:@"content"];
    if (content != nil) {
        
        
        CGRect rect = [content boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return 50 + rect.size.height + 20;
    } else {
        return 50 + 20;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellPool = @"cellPool";
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    ReViewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellPool];
    if (cell == nil) {
        cell = [[[ReViewsCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellPool] autorelease];
    }
    NSDictionary *review = _array[indexPath.row];
    cell.contentLabel.text = [review objectForKey:@"content"];
    NSDictionary *user = [review objectForKey:@"user"];
    cell.userName.text = [user objectForKey:@"name"];
    [cell.userImage sd_setImageWithURL:[NSURL URLWithString:[user objectForKey:@"avatar_l"]] placeholderImage:image];
    
    return cell;
}
-(void)viewDidLayoutSubviews
{
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
@end
